package io.github.talelin.latticy.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.annotation.ScopeLevel;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.FlowerDTO;
import io.github.talelin.latticy.entity.Flower;
import io.github.talelin.latticy.entity.SendCollect;
import io.github.talelin.latticy.model.FlowerDO;
import io.github.talelin.latticy.model.NewUserDO;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.service.FlowerService;
import io.github.talelin.latticy.service.SendCollectService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/api/flower")
@PermissionModule("花型管理")
public class FlowerController
{
    @Autowired
    private FlowerService flowerService;

    @Autowired
    private SendCollectService sendCollectService;

    /**
     * 送收花详情
     */
    @GetMapping("/{id}")
    @ScopeLevel
    public SendCollect detail(@PathVariable @Positive Long id)
    {
        return sendCollectService.getDetail(id);
    }

    /**
     * 选择花型
     */
    @GetMapping("/all")
    public List<Flower> all()
    {
        return flowerService.getAll();
    }



    /******************************** 后台 **********************************/
    @PostMapping("")
    @PermissionMeta(value = "添加花型")
    @GroupRequired
    public CreatedVO create(@RequestBody @Validated FlowerDTO dto)
    {
        flowerService.create(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @PermissionMeta(value = "编辑花型")
    @GroupRequired
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Long id,
                            @RequestBody @Validated FlowerDTO dto)
    {
        flowerService.update(id, dto);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @PermissionMeta(value = "删除花型")
    @GroupRequired
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Long id)
    {
        flowerService.delete(id);
        return new DeletedVO();
    }

    @GetMapping("/detail/{id}")
    public FlowerDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Long id)
    {
        return flowerService.get(id);
    }

    @GetMapping("/page")
    public PageResponseVO<FlowerDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Long count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Long page
    ) {
        Page<FlowerDO> pager = new Page<>(page, count);
        IPage<FlowerDO> paging = flowerService.getBaseMapper().selectPage(pager, null);
        return new PageResponseVO<>(paging.getTotal(), paging.getRecords(), paging.getCurrent(), paging.getSize());
    }


}
